package com.wwwarehouse.resource_center.fragment.createobject.creategoods;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.activity.base.BaseActivity;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.tools.LogUtils;
import com.wwwarehouse.resource_center.R;
import com.wwwarehouse.resource_center.bean.goods.CategoryBean;
import com.wwwarehouse.resource_center.bean.goods.GoodAddCategoryBean;
import com.wwwarehouse.resource_center.constant.ResourceConstant;
import com.wwwarehouse.resource_center.customView.AutoClickButton;
import com.wwwarehouse.resource_center.eventbus_event.CategoryEvent;
import com.wwwarehouse.resource_center.fragment.rules.TheParentFragment;

import java.util.HashMap;
import java.util.Map;

import de.greenrobot.event.EventBus;

/**
 * @author Joseph on 2017/9/14.
 *         <p/>
 *         添加类目
 */

public class GoodsCategoryAddFragment extends TheParentFragment {

    private AutoClickButton mBtn;
    private EditText mEtText;
    public static final int INSERT_CATEGORY = 1;  //添加类目
    private CategoryBean.EmptyIdentifierBean bean;
    private String ownerUkid;

    @Override
    protected int getContentId() {
        return R.layout.fragment_goods_add_category;
    }

    @Override
    protected void init(View view) {
        mBtn = findView(view, R.id.confirm_btn);
        mEtText = findView(view, R.id.set_rule_name);
        mBtn.bindEditText(mEtText);
        ((BaseActivity) mActivity).hideSoftByEditViewIds(new int[]{R.id.set_rule_name});

        Bundle bundle = getArguments();
        if (bundle == null)
            return;
        bean = bundle.getParcelable("bean");
        ownerUkid = bundle.getString("ownerUkid");
    }


    @Override
    protected void loadDatas() {
        mBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //添加类目
                if (bean == null)
                    return;
                Map<String, Object> map = new HashMap<>();
                map.put("categoryName", mEtText.getText().toString().trim());
                map.put("categoryTreeId", bean.getCategoryTreeUkid());
                map.put("businessId", ownerUkid);
                map.put("parentMetaCategoryUkid", bean.getMetaCategoryUkid());
                LogUtils.showInfoLog("INSERT_CATEGORY:" + INSERT_CATEGORY);
                httpPost(ResourceConstant.INSERT_CATEGORY, map, INSERT_CATEGORY, true, "");
            }
        });
    }


    @Override
    public void setTitle() {
        if (peekFragment() instanceof GoodsCategoryAddFragment)
            mActivity.setTitle(getString(R.string.add_category));
    }

    @Override
    public void requestDatas() {

    }

    @Override
    public void onSuccess(CommonClass commonClass, int what) {
        if (INSERT_CATEGORY == what) {
            if (TextUtils.equals("0", commonClass.getCode())) {
                try {
                    GoodAddCategoryBean.DataBean goodAddCategoryBean = JSON.parseObject(commonClass.getData().toString(), GoodAddCategoryBean.DataBean.class);
                    CategoryBean.EmptyIdentifierBean emptyIdentifierBean = new CategoryBean.EmptyIdentifierBean();
                    emptyIdentifierBean.setCategoryTreeUkid(goodAddCategoryBean.getCategoryTreeUkid());
                    emptyIdentifierBean.setCategoryUkid(goodAddCategoryBean.getCategoryUkid());
                    emptyIdentifierBean.setChildNodeCount("0");
                    emptyIdentifierBean.setLeafNode(goodAddCategoryBean.getLeafNode());
                    emptyIdentifierBean.setLevel(goodAddCategoryBean.getLevel());
                    emptyIdentifierBean.setMetaCategoryUkid(goodAddCategoryBean.getMetaCategoryUkid());
                    emptyIdentifierBean.setName(mEtText.getText().toString().trim());
                    emptyIdentifierBean.setSource(goodAddCategoryBean.getSource());
                    emptyIdentifierBean.setParentMetaCategoryUkid(goodAddCategoryBean.getParentMetaCategoryUkid());

                    EventBus.getDefault().post(new CategoryEvent(emptyIdentifierBean, "finishAdd"));

                } catch (Exception e) {

                }

            } else {
                toast(commonClass.getMsg());
            }
        }
    }

    @Override
    public void onReLoad(int code) {

    }

}
